<template>
  <div class="formView">
    <div class="wrap">
      <div id="cornerQrcode" class="share" @click="showQrcode">
        <div id="qrCode" ref="qrCodeDiv" v-show="isShowQrcode"></div>   
      </div>
      <p class="title"></p>
      <div class="content" v-if="!isSubmit">
        <div class="formHead">
          <h2>{{formHead.formHead}}</h2>
          <div class="desc">{{formHead.formDesc}}</div>
        </div>
        <viewItem :editList="editList" ref="submitData" />
        <div class="pcsubmint" v-if="!formHead.BTNHIDE">
          <el-button type="primary" @click="submit" :disabled="disabledBtn">{{formHead.BTNTXT}}</el-button>
        </div>
      </div>
      <div class="SubmitInfo" v-else>提交成功！</div>
    </div>
  </div>
</template>
<script>
import QRCode from "qrcodejs2";
import viewItem from "@/components/formView/viewItem.vue";
export default {
  name: "FormView",
  components: { viewItem },
  data() {
    return {
      formHead: {},
      editList: [],
      isShowQrcode: false,
      isSubmit: false,
      disabledBtn:false
    };
  },
  async mounted() {
    let id = this.$route.query.id;
    let res = await this.$api.info({ id });
    //console.log(res);
    this.formHead = res.M;
    this.editList = res.F;

    this.$nextTick(function() {
      this.bindQRCode();
    });
  },
  methods: {
    showQrcode() {
      this.isShowQrcode = this.isShowQrcode === true ? false : true;
    },
    bindQRCode: function() {
      new QRCode(this.$refs.qrCodeDiv, {
        text: window.location.href,
        width: 200,
        height: 200,
        colorDark: "#333333", //二维码颜色
        colorLight: "#ffffff", //二维码背景色
        correctLevel: QRCode.CorrectLevel.L //容错率，L/M/H
      });
    },
    async submit() {
      try{
        let submitData = await this.$refs.submitData.submit();
        this.disabledBtn = true;
        await this.$api.submit({
          FORMID: this.$route.query.id,
          DATA: submitData
        });
       this.isSubmit = true;
      }catch(e){
        console.error(e);
      }
      this.disabledBtn = false;

    }
  }
};
</script>
<style lang="scss" scoped>
.formView {
  width: 100%;
  height: 585px;
  background: #eee;
  padding: 20px 0;
  .wrap {
    width: 640px;
    margin: 0 auto;
    padding-bottom: 20px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
    position: relative;
    opacity: 1;
    filter: Alpha(Opacity=100);
    border-radius: 6px;
    .share {
      background: rgba(153, 153, 153, 0.4) url(../assets/qrcode_icon.png)
        no-repeat center center;
      width: 40px;
      height: 40px;
      right: -50px;
      position: absolute;
      background-size: 70% auto;
      border-radius: 4px;
      cursor: pointer;
      .qrcode-container {
        position: absolute;
        top: 0;
        right: -80px;
        text-indent: 0;
        background-color: #fff;
        box-shadow: 0 5px 7px 0 rgba(0, 0, 0, 0.2);
        border: 1px solid #ccc;
        .qrcode {
          width: 120px;
          height: 120px;
          cursor: pointer;
        }
      }
    }
    .title {
      width: 100%;
      height: 40px;
      background: #ccc;
    }
    .content {
      margin: 20px 15px;
      font-size: 14px;
      .formHead {
        padding: 0 6px 10px 6px;
        border-bottom: 1px dotted #ccc;
        h2 {
          line-height: 36px;
        }
        .desc {
          line-height: 24px;
        }
      }
      .pcsubmint {
        margin: 20px 0;
        button {
          display: block;
          margin: 0 auto;
        }
      }
    }
    .SubmitInfo {
      height: 160px;
      text-align: center;
      line-height: 160px;
    }
  }
}
</style>